<template>
  <el-card style="width: 500px">
    <el-upload
        class="avatar-uploader"
        action="http://localhost:9876/sys-file/upload"
        :show-file-list="false"
        :on-success="handleAvatarSuccess">
      <img v-if="from.avatar" :src="from.avatar" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>

    <el-form label-width="80px" size="small">
      <el-form-item label="用户名">
        <el-input v-model="from.userName" disabled placeholder="请输入用户名" maxlength="30"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="from.nickName" placeholder="请输入用户昵称" maxlength="30"></el-input>
      </el-form-item>
      <el-form-item label="性别">
        <el-select v-model="from.sex" placeholder="请选择性别">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="from.phonenumber" placeholder="请输入用户电话" maxlength="11"></el-input>
      </el-form-item>
      <el-form-item label="邮箱">
        <el-input v-model="from.email" placeholder="请输入用户邮箱" maxlength="30"></el-input>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="from.remark" type="textarea" placeholder="请输入内容"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: "Person",
  data() {
    return {
      from: {},
      options: [{
        value: '0',
        label: '男'
      }, {
        value: '1',
        label: '女'
      }, {
        value: '2',
        label: '未知'
      }],
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {}
    }
  },
  created() {
    this.getUser().then(res => {
      this.from = res;
    })
  },
  methods: {
    async getUser() {
      return (await this.request.get("/user/username/" + this.user.userName)).data
    },
    save() {
      this.request.post("/user", this.from).then(res => {
        if (res.code === 200) {
          this.$message.success("保存成功")

          // 触发父级更新User的方法
          this.$emit("refreshUser")

          // 更新浏览器存储的用户信息
          this.getUser().then(res => {
            res.token = JSON.parse(localStorage.getItem("user")).token
            localStorage.setItem("user", JSON.stringify(res))
          })
        } else {
          this.$message.error("保存失败")
        }
      })
    },
    handleAvatarSuccess(res) {
      this.from.avatar = res;
    }
  }
}
</script>

<style>
.avatar-uploader {
  text-align: center;
  padding-bottom: 10px;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}

.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
</style>
